<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript">
	$(function(){
		getUseInfoData();
		mySubmit("useInfo",initUseInfo,successUseInfo);
	})
	
	//获取建筑数量下的使用功能集合
		function getUseInfoData() {
			var obj;
			var url = "bulidingNum/useInfo/getUseInfoByBId";
			//pid:建筑数量id 预留
			var bid = $("#buildingNumId").val();
			if(bid==null || bid==""){
				return false;
			}
			$.getJSON(url, {
				buildingNumId : bid
			}, function(data, status) {
				//console.log(data);
				//if (status == "success") {

					//将用途集合数据拆分成地上、地下两个数组
					var groundUse = new Array();
					var undergroundUse = new Array();
					if(data.length>0){
						var $t,v;
						//循环添加模板div
						for(var i=0; i<data.length;i++){
							if(data[i].site==0){//地上
								addDiv3(obj,1);
								groundUse.push(data[i]);
							}else{//地下
								addDiv3(obj,2);
								undergroundUse.push(data[i]);
							}
						}
						//循环赋值
						$t=$("#use_1 table");
						for(var i=0;i<$t.length;i++){
							v=$t[i];
							$(v).find("input[name='purpose']").val(groundUse[i].purpose);
							$(v).find("input[name='area']").val(returnFloat(groundUse[i].area));
							$(v).find("input[name='scope']").val(groundUse[i].scope);
							$(v).find("input[name='leasePrice']").val(returnFloat(groundUse[i].leasePrice));
							$(v).find("input[name='servicePrice']").val(returnFloat(groundUse[i].servicePrice));
							$(v).find("input[name='id']").val(groundUse[i].id);
							$(v).find("select[name='lpriceType']").val(groundUse[i].lpriceType).trigger("change");;
							$(v).find("select[name='spriceType']").val(groundUse[i].spriceType).trigger("change");;
						}
						var $r=$("#use_2 table");
						for(var i=0;i<$r.length;i++){
							v=$r[i];
							$(v).find("input[name='purpose']").val(undergroundUse[i].purpose);
							$(v).find("input[name='area']").val(returnFloat(undergroundUse[i].area));
							$(v).find("input[name='scope']").val(undergroundUse[i].scope);
							$(v).find("input[name='leasePrice']").val(returnFloat(undergroundUse[i].leasePrice));
							$(v).find("input[name='servicePrice']").val(returnFloat(undergroundUse[i].servicePrice));
							$(v).find("input[name='id']").val(undergroundUse[i].id);
							$(v).find("select[name='lpriceType']").val(undergroundUse[i].lpriceType).trigger("change");;
							$(v).find("select[name='spriceType']").val(undergroundUse[i].spriceType).trigger("change");;
						}
						//设置输入框为只读
					$("#useInfo").find("input,textarea").attr("readonly",true);
					$("#useInfo .icon-fullscreen").hide();
					$("#useInfo").find(".addbtn,select").attr("disabled",true);
					//隐藏按钮
					$("#useInfo #btn_div2").show();
					$("#useInfo #btn_div1").hide();
					}else{
						$("#useInfo #btn_div2").hide();
						$("#useInfo #btn_div1").show();
					
					}
					
				//}
			});
		}
	
	var optUseInfo;
	//optUseInfo操作字段，0：临时保存；1：新增；2：修改
	function saveUseInfo(val){
		optUseInfo=val;
		if(val==3){
			//解除锁定状态
			var index=layer.confirm('确定开启编辑状态？', {
			 	offset:'20%',
				shade:0
			}, function(){
			  //去除只读开启按钮编辑
			   $("#useInfo").find("input,textarea").attr("readonly",false);
			   $("#useInfo #modifyBtn").hide();
			  // $(".hidbtn").removeClass("hidbtn");
			  $("#useInfo .hidbtn").addClass("showbtn");
			  $("#useInfo .icon-fullscreen").show();
			   $("#useInfo").find(".addbtn,select").attr("disabled",false);
			   layer.close(index);
			});
		}else{
			
			$("#useInfo").submit();
		}
	}
</script>
<form class="cmxform" id="useInfo" method="post" action="bulidingNum/useInfo/add">
	<div style="padding:0px">
	<!-- 使用功能start -->
	<div class="panel panel-default">
   			<div class="panel-body">
   				<!-- 地上用途start -->
   				<div id="use_1">
	   				<p class="style_border_bottom"><span class="style_btn">地上</span>
	   					<span class="style_input">&nbsp;
							<a class="btn btn-danger radius a_font addbtn"  onclick="addDiv3(this,'1')"> 添加</a>
	   					</span>
	   				</p>
	   				
				</div>
   				<!-- 地上用途end -->
   				
   				<!-- 地下用途start -->
   				<div id="use_2">
	   				<p class="style_border_bottom"><span class="style_btn">地下</span>
	   					<span class="style_input">&nbsp;
							<a class="btn btn-danger radius a_font addbtn"  onclick="addDiv3(this,'2')"> 添加</a>
	   					</span>
	   				</p>
	   				
				</div>
   				<!-- 地下用途end -->
			</div>
			</div>
			<!-- 使用功能end -->
			<div style="width:100%;text-align:center; padding-top:20px">
				<div id="btn_div1">
					<button class="btn btn-danger radius my-btn"
						onclick="saveUseInfo(0)" type="button">提交</button>
					<button class="btn btn-danger radius my-btn"
						onclick="saveUseInfo(1)" type="button">保 存</button>
					<a class="btn btn-default radius" onClick="resetForms('useInfo');">
						重置 </a>
					<br />
				</div>
				<div id="btn_div2" style="display:none;">
					<shiro:hasPermission name="permission:edit">
						<button id="modifyBtn" type="button" class="btn btn-danger radius my-btn"
							onclick="saveUseInfo(3)">启用编辑</button>
						<button class="btn btn-danger radius my-btn hidbtn"
							onclick="saveUseInfo(2)" type="button">保存修改</button>
						<button class="btn btn-default radius hidbtn" onClick="resetForms('useInfo');">
							重置 </button>
					</shiro:hasPermission>	
				</div>
	    	</div> 
	    	 
		</div>
</form>

<script type="text/javascript">
	$(function(){
		$(".a_font").css("color","#fff");
	});

	function addDiv3(obj,i){
		var	node ;
		node = $("#yt").html();
		var oTest = $("#use_"+i); 
		oTest.append(node);
		$("#use_"+i).find("select[name='spriceType']").select2();
		$("#use_"+i).find("select[name='lpriceType']").select2();
	}
	//存储将要删除的id，传到后台删除
	var delUseInfoIds = new Array();
	function delTrDiv2(obj) {
		var id = $(obj).parent().parent().parent().parent().find("input[name='id']").val();
		delUseInfoIds.push(id);
		$(obj).parent().parent().parent().parent().parent().remove();
		layer.msg("点击保存后，将彻底删除该条数据！");
	}
	var ui;
	//封装表单数据为json
	function initUseInfo(){
		ui = layer.load(2);
		//封装地上数据
		var $t=$("#use_1 table");
		var ground=new Array();
		for(var i=0;i<$t.length;i++){
			var v=$t[i];
			var purpose=$(v).find("input[name='purpose']").val();
			var area=$(v).find("input[name='area']").val();
			var scope=$(v).find("input[name='scope']").val();
			var leasePrice=$(v).find("input[name='leasePrice']").val();
			var servicePrice=$(v).find("input[name='servicePrice']").val();
			var lpriceType=$(v).find("select[name='lpriceType']").val();
			var spriceType=$(v).find("select[name='spriceType']").val();
			var id=$(v).find("input[name='id']").val();
			var site = "0";//0 地上  1 地下
			if (!testNum(area)) {
				layer.msg("地上第" + (i + 1) + "行面积请输入数值");
				$(v).find("input[name='area']")[0].focus();
				layer.close(ui);
				return null;
			}else if(!testNum(leasePrice)) {
				layer.msg("地上第" + (i + 1) + "行租赁价格请输入数值");
				$(v).find("input[name='leasePrice']")[0].focus();
				layer.close(ui);
				return null;
			}else if(!testNum(servicePrice)) {
				layer.msg("地上第" + (i + 1) + "行服务费价格请输入数值");
				$(v).find("input[name='servicePrice']")[0].focus();
				layer.close(ui);
				return null;
			}
			ground.push({id:id,purpose:purpose,area:area,scope:scope,leasePrice:leasePrice,servicePrice:servicePrice,site:site,fkUseInfo:'${requestScope.buildingNum.useInfo}',lpriceType:lpriceType,spriceType:spriceType});
		}
		
		
		//封装地下数据
		var $t=$("#use_2 table");
		for(var i=0;i<$t.length;i++){
			var v=$t[i];
			var purpose=$(v).find("input[name='purpose']").val();
			var area=$(v).find("input[name='area']").val();
			var scope=$(v).find("input[name='scope']").val();
			var leasePrice=$(v).find("input[name='leasePrice']").val();
			var servicePrice=$(v).find("input[name='servicePrice']").val();
			var lpriceType=$(v).find("select[name='lpriceType']").val();
			var spriceType=$(v).find("select[name='spriceType']").val();
			var id=$(v).find("input[name='id']").val();
			var site = "1";//0 地上  1 地下
			if (!testNum(area)) {
				layer.msg("地下第" + (i + 1) + "行面积请输入数值");
				$(v).find("input[name='area']")[0].focus();
				layer.close(ui);
				return null;
			}else if(!testNum(leasePrice)) {
				layer.msg("地下第" + (i + 1) + "行租赁价格请输入数值");
				$(v).find("input[name='leasePrice']")[0].focus();
				layer.close(ui);
				return null;
			}else if(!testNum(servicePrice)) {
				layer.msg("地下第" + (i + 1) + "行服务费价格请输入数值");
				$(v).find("input[name='servicePrice']")[0].focus();
				layer.close(ui);
				return null;
			}
			ground.push({id:id,purpose:purpose,area:area,scope:scope,leasePrice:leasePrice,servicePrice:servicePrice,site:site,fkUseInfo:'${requestScope.buildingNum.useInfo}',lpriceType:lpriceType,spriceType:spriceType});
		}
		
		var formData={
				useInfo2:ground,
				opt:optUseInfo,
				fkUseInfo:'${requestScope.buildingNum.useInfo}',
				delUseInfoIds:delUseInfoIds
		};
		return formData;		
	}
	
	//成功后调用方法
	function successUseInfo(responseText){
		//清空数组
		delUseInfoIds = [];
		layer.close(ui);
		if(responseText.success){
        	/* getUseInfoData();
        	$("#useInfo #use_1>div").remove();
        	$("#useInfo #use_2>div").remove();
        	$("#useInfo #modifyBtn").show();
        	$("#useInfo .hidbtn").removeClass("showbtn");
        	//显示当前tab
			$("#BN>li:eq(2) a").tab("show"); */
			layer.alert("保存成功！", {
					offset:'20%',
					shade:0.1
				}, function() {
				parent.tabTypeShow=2;
				location.reload();
			});
        	layer.close(ui);
		}else{
			alert('保存失败！');
			layer.closeAll();
		}
	}
	
</script>

<!-- 模板start -->
<div id="yt" style="display:none;">
		<div style="border:1px solid gray;padding:10px;margin:10px">
			<table cellpadding="10" cellspacing="5" class="mytable" >
				<tr class="style_border_bottom_solid">
	                 <td width="90" class="td-right">
	                     <span class="style_btn">用途</span>
	                 </td>
	                 <td>
	                 	<input id="" name="purpose" value=""/>
	                 </td>
	                 <td colspan="2" valign="top" align="right">
	                	<a href="javascript:void(0)" onclick="delTrDiv2(this)">
	                      <i class="icon-fullscreen"></i>
	                  	</a>
	                 </td>
	             </tr>
	              <tr>
	                  <td width="90" class="td-right">
	                      <span style="padding-left: 20px;">面积</span>
	                  </td>
	                  <td>
	                  	<input id="" name="area" value=""/> ㎡
	                  </td>
	                  <td width="90" class="td-right">
	                      <span>范围</span>
	                  </td>
	                  <td>
	                  	<input id="" name="scope" value=""/>
	                  </td>
	             </tr>
	             <tr>
	                  <td width="90" class="td-right">
	                      <span style="padding-left: 20px;">租赁价格</span>
	                  </td>
	                  <td>
	                  	<input id="" name="leasePrice" value=""/> 
	                  	<select name="lpriceType"  style="width:80px">
							<option value="0">元/时</option>
							<option value="1">元/天</option>
							<option value="2">元/月</option>
							<option value="3">元/年</option>
						</select>
	                  </td>
	                  <td width="90" class="td-right">
	                      <span>服务费价格</span>
	                  </td>
	                  <td>
	                  	<input id="" name="servicePrice" value=""/> 
	                  	<select name="spriceType"  style="width:80px">
							<option value="0">元/时</option>
							<option value="1">元/天</option>
							<option value="2">元/月</option>
							<option value="3">元/年</option>
						</select>
	                  	<input type="hidden" id="entityId_id" name="id" value=""/>
	                  </td>
	             </tr>
			</table>
		</div>
	</div>
	
<!-- 模板end -->

